#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "JQ8900.h"
#include "driver/gpio.h"
#include <string.h>

static QueueHandle_t uart_queue;


void User_UART0_Init(int baud_rate)
{
    uart_config_t uart_structue = {
        .baud_rate = baud_rate,
        .data_bits = UART_DATA_8_BITS,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .parity = UART_PARITY_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
        .stop_bits = UART_STOP_BITS_1,
    };
    uart_param_config(JQ8900_UART,&uart_structue);
    uart_set_pin(JQ8900_UART,USER_TX_JQ,USER_RX_JQ,UART_PIN_NO_CHANGE,UART_PIN_NO_CHANGE);
    uart_driver_install(JQ8900_UART,1024,1024,20,&uart_queue,0);
}


//发送命令
//param：待发送的命令
//return_val：无
void My_Uart_Sendcmd(uint32_t cmd)
{
    uart_write_bytes(JQ8900_UART,&cmd,sizeof(cmd));
}

void Uart_Send1Byte(uint8_t byte)
{
    uart_write_bytes(JQ8900_UART,&byte,sizeof(byte));
}


//设置音量 0~22
//param：音量
//return_val：无
void Vol_Set(uint8_t vol_num)
{
    uint8_t check = 0;
    Uart_Send1Byte(0xAA);
    Uart_Send1Byte(0x13);
    Uart_Send1Byte(0x01);
    uart_write_bytes(JQ8900_UART,&vol_num,sizeof(vol_num));
    check = 0xAA + 0x13 + 0x01 + vol_num;
    Uart_Send1Byte(check);
}


//设置播放的曲目
//param：待播放的曲目
//return_val：无
void Specify_Repertoire(uint16_t num)
{
    uint8_t check = 0;
    Uart_Send1Byte(0xAA);
    Uart_Send1Byte(0x07);
    Uart_Send1Byte(0x02);
    uart_write_bytes(JQ8900_UART,(uint8_t *)(&num) + 1,1);
    uart_write_bytes(JQ8900_UART,(uint8_t *)(&num),1);
    check = 0xAA + 0x07 + 0x02 + num;
    Uart_Send1Byte(check);
}

